//
//  HistorySearchTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/12.
//

import UIKit

protocol HistorySearchDelegate{
    func didSelectSearchHistoryItem(index: NSInteger,tagTitle: String)
}

class HistorySearchTableViewCell: UITableViewCell {
    var tagListView: YZTagList!
    var dataArray: [String] = []
    var delegate: HistorySearchDelegate!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        //标签
        tagListView = YZTagList.init(frame: CGRect.init(x: 0, y:  8, width: self.frame.size.width - 24, height: 32))
        tagListView.lineNum = 0
        tagListView.tagCornerRadius = 16
        tagListView.tagMargin = 10  //外边距
        tagListView.tagButtonMargin = 10 //上下左右内边距
        tagListView.xt_tagMarginW = 10  //左右内边距
        tagListView.tagBackgroundColor = UIColor(hex: 0xF8F9FA)
        tagListView.tagColor = Theme.titleLabColor
        tagListView.tagFont = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        tagListView.showTouchColor = false
        self.contentView.addSubview(tagListView)
     
        //点击TagList的返回代理
        tagListView.clickTagBlock = {[weak self] (title, index, btn) in
            if index < (self?.dataArray.count)! {
                self?.delegate?.didSelectSearchHistoryItem(index: index, tagTitle: (self?.dataArray[index])!)
            }
        }
       
        
        self.tagListView.snp.makeConstraints { make in
            make.top.equalTo(0).offset(8)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-20)
        }
        
    }
    
    
    func setHistoryData(data: NSArray) {
        self.dataArray = []
        let maxCount = min(30, data.count)//取30条
        for index in 0 ..< maxCount {
            let tip = data[index] as! String
            dataArray.append(tip)
        }
        self.tagListView.addTags(dataArray)  //赋值
        self.tagListView.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(self.tagListView.frame.size.height)
        }
      
        for item in self.tagListView.tagButtons {
            let btn = item as! UIButton
            btn.layer.borderWidth = 1
            btn.layer.cornerRadius = 16
            btn.height = 32
            btn.layer.borderColor = UIColor(hex: 0x939AA8,alpha: 0.12).cgColor
            btn.setTitleColor(Theme.titleLabColor, for: .normal)
        }
        
        self.updateConstraints()
    }

    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
